﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

#endregion

namespace ScrumTable.UI.View.Converter
{

    public enum CalcType
    {
        Add,
        Sub,
        Multi,
        Div
    }
    /// <summary>
    /// Converts a Grid view 
    /// </summary>
    [ValueConversion(typeof(double), typeof(double))]
    public class CalcConverter : IValueConverter
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        public CalcType CalcType { get; set; }

        public double ValueRight { get; set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        public CalcConverter()
        {
            CalcType = CalcType.Add;
            ValueRight = 1;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
        /// <summary>
        /// Converts given bool value to visibility.
        /// true = returns the parameter as grid value 
        /// false = returns 0 
        /// </summary>
        /// <param name="value">The bool value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double valueleft;
            if(value is Control)
            {
                valueleft = Math.Min(((Control) value).ActualHeight, ((Control) value).ActualWidth);
                valueleft = Math.Max(valueleft, 1);
            }
            else if (value is double)
            {
                valueleft = (double)value;    
            }
            else
            {
                valueleft = 0;
            }
            

            switch(CalcType)
            {
                case CalcType.Add:
                    return valueleft + ValueRight;
                case CalcType.Sub:
                    return valueleft - ValueRight;
                case CalcType.Multi:
                    return valueleft * ValueRight;
                case CalcType.Div:
                    return valueleft / ValueRight;
            }
            return 0.0;
        }

        /// <summary>
        /// Not supported. Throws NotSupportedException.
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();

        }
       
        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion

       
    }
}
